package com.hmyd.hmyd_android.ui;

import java.util.ArrayList;
import java.util.List;

import android.graphics.SumPathEffect;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TableLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.adapter.FinancingDetailsListAdapter;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.CapitalFlows;
import com.hmyd.hmyd_android.dto.FlowSummary;
import com.hmyd.hmyd_android.dto.Page;
import com.hmyd.hmyd_android.utils.Utils;
import com.hmyd.hmyd_android.view.listview.XListView;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 我的回款列表
 * @author Administrator
 *
 */
@ContentView(R.layout.activity_back_list)
public class MyBackActivity extends BasicListActivity {
	private static final int REFRESH = 1;
	private static final int LOADMORE = 2;
	private static final int FLOWSUMMARY = 3;

	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;

	@ViewInject(R.id.list)
	private XListView mList;

	protected boolean hasMore = false;
	private String cursorId;

	private FinancingDetailsListAdapter adapter;
	private List<CapitalFlows> listData = new ArrayList<CapitalFlows>();
	
	@ViewInject(R.id.content)
	private TableLayout mSummaryLayout;
	@ViewInject(R.id.summary_time)
	private TextView mSummaryTime;

	public XListView getXList() {
		return mList;
	}

	@Override
	protected void onResume() {
		super.onResume();

		if (!isLoading && listData.size() == 0) {
			RequestParams params = new RequestParams();
			params.addBodyParameter("items", "back");

			onRequest(REFRESH, UrlConstant.CAPITALFLOWS, HttpMethod.POST, params);
			
			onRequest(FLOWSUMMARY, UrlConstant.FLOWSUMMARY, HttpMethod.POST, params);
		}
	}


	@Override
	public void onRefresh() {
		super.onRefresh();

		RequestParams params = new RequestParams();
		params.addBodyParameter("items", "back");

		onRequest(REFRESH, UrlConstant.CAPITALFLOWS, HttpMethod.POST, params);
		onRequest(FLOWSUMMARY, UrlConstant.FLOWSUMMARY, HttpMethod.POST, params);
	}

	@Override
	public void onLoadMore() {
		super.onLoadMore();

		RequestParams params = new RequestParams();
		params.addBodyParameter("items", "back");
		params.addBodyParameter("lastCursor", cursorId);

		onRequest(LOADMORE, UrlConstant.CAPITALFLOWS, HttpMethod.GET, params);
	}

	@Override
	public void onPostSuccess(int postId, String msg) {
		super.onPostSuccess(postId, msg);

		Gson gson = new Gson();
		java.lang.reflect.Type type2 = new TypeToken<Page<CapitalFlows>>() {}.getType();
		Page<CapitalFlows> page = gson.fromJson(msg, type2);
		hasMore = page.hasMore;

		if(page!=null && page.records!=null){
			switch (postId) {
			case REFRESH:
				listData.clear();
				listData.addAll(page.records);
				break;
			case LOADMORE:
				listData.addAll(page.records);
				break;
			default:
				break;
			}

			if(listData.size()>0){
				cursorId = listData.get(listData.size()-1).cursorId;
			}else{
				cursorId = null;
			}

			adapter.notifyDataSetChanged();
		}

		if(getXList()!=null && hasMore){
			getXList().setPullLoadEnable(true);
		}else if(getXList()!=null && !hasMore){
			getXList().setPullLoadEnable(false);
		}
		
		if(postId==FLOWSUMMARY){//总计汇总
			Gson g = new Gson();
			System.out.println("get FLOWSUMMARY:"+msg);
			java.lang.reflect.Type flowType = new TypeToken<FlowSummary>() {}.getType();
			FlowSummary flowSummary = g.fromJson(msg, flowType);
			
			stepFlowSummary(flowSummary);
			
		}
	}

	@Override
	public void onPostFailure(int postId, String msg) {
		super.onPostFailure(postId, msg);
		if (getXList() != null && hasMore) {
			getXList().setPullLoadEnable(true);
		} else if (getXList() != null && !hasMore) {
			getXList().setPullLoadEnable(false);
		}
	}

	@Override
	protected void setParams() {
		home_title_txt.setText(getResources().getString(R.string.back));
		home_title.setOnTouchListener(touch);
		home_right.setVisibility(View.INVISIBLE);

		mList.setPullRefreshEnable(true); // 允许下拉刷新
		mList.setPullLoadEnable(false); // 禁止加载更多
		mList.setAutoLoadEnable(false); // 禁止自动加载
		mList.setXListViewListener(this);// 加载监听

		adapter = new FinancingDetailsListAdapter(this, listData);
		mList.setAdapter(adapter);
	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {

	}
	
	/**
	 * 设置底部总计汇总数据
	 * @param flowSummary
	 */
	private void stepFlowSummary(FlowSummary flowSummary){
		mSummaryTime.setText("元");
		
		if(flowSummary==null){
			return;
		}
		
		mSummaryLayout.removeAllViews();
		
		if(flowSummary.deposit!=0){
			LayoutInflater mInflater = LayoutInflater.from(this);
			View view = mInflater.inflate(R.layout.view_summary_item, null);
			TextView descTx = (TextView)view.findViewById(R.id.desc);
			TextView amountTx = (TextView)view.findViewById(R.id.amount);
			
			
			descTx.setText("充值总额：");
			amountTx.setText(Utils.amountConversionFormat(flowSummary.deposit)+"元");
			
			mSummaryLayout.addView(view);
		}
		
		if(flowSummary.currency!=0){
			LayoutInflater mInflater = LayoutInflater.from(this);
			View view = mInflater.inflate(R.layout.view_summary_item, null);
			TextView descTx = (TextView)view.findViewById(R.id.desc);
			TextView amountTx = (TextView)view.findViewById(R.id.amount);
			
			
			descTx.setText("存钱罐收益总额：");
			amountTx.setText(Utils.amountConversionFormat(flowSummary.currency)+"元");
			
			mSummaryLayout.addView(view);
		}
		
		if(flowSummary.recommend!=0){
			LayoutInflater mInflater = LayoutInflater.from(this);
			View view = mInflater.inflate(R.layout.view_summary_item, null);
			TextView descTx = (TextView)view.findViewById(R.id.desc);
			TextView amountTx = (TextView)view.findViewById(R.id.amount);
			
			
			descTx.setText("好友推荐总额：");
			amountTx.setText(Utils.amountConversionFormat(flowSummary.recommend)+"元");
			
			mSummaryLayout.addView(view);
		}
		
		if(flowSummary.back_interest!=0){
			LayoutInflater mInflater = LayoutInflater.from(this);
			View view = mInflater.inflate(R.layout.view_summary_item, null);
			TextView descTx = (TextView)view.findViewById(R.id.desc);
			TextView amountTx = (TextView)view.findViewById(R.id.amount);
			
			
			descTx.setText("投资利息回款总额：");
			amountTx.setText(Utils.amountConversionFormat(flowSummary.back_interest)+"元");
			
			mSummaryLayout.addView(view);
		}
		
		if(flowSummary.back_coupon!=0){
			LayoutInflater mInflater = LayoutInflater.from(this);
			View view = mInflater.inflate(R.layout.view_summary_item, null);
			TextView descTx = (TextView)view.findViewById(R.id.desc);
			TextView amountTx = (TextView)view.findViewById(R.id.amount);
			
			descTx.setText("投资现金券回款总额：");
			amountTx.setText(Utils.amountConversionFormat(flowSummary.back_coupon)+"元");
			
			mSummaryLayout.addView(view);
		}
		
		if(flowSummary.back_captial!=0){
			LayoutInflater mInflater = LayoutInflater.from(this);
			View view = mInflater.inflate(R.layout.view_summary_item, null);
			TextView descTx = (TextView)view.findViewById(R.id.desc);
			TextView amountTx = (TextView)view.findViewById(R.id.amount);
			
			
			descTx.setText("投资本金回款总额：");
			amountTx.setText(Utils.amountConversionFormat(flowSummary.back_captial)+"元");
			
			mSummaryLayout.addView(view);
		}
		
		if(flowSummary.deductcoupon!=0){
			LayoutInflater mInflater = LayoutInflater.from(this);
			View view = mInflater.inflate(R.layout.view_summary_item, null);
			TextView descTx = (TextView)view.findViewById(R.id.desc);
			TextView amountTx = (TextView)view.findViewById(R.id.amount);
			
			
			descTx.setText("现金券抵扣总额：");
			amountTx.setText(Utils.amountConversionFormat(flowSummary.deductcoupon)+"元");
			
			mSummaryLayout.addView(view);
		}
		
		if(flowSummary.deduct!=0){
			LayoutInflater mInflater = LayoutInflater.from(this);
			View view = mInflater.inflate(R.layout.view_summary_item, null);
			TextView descTx = (TextView)view.findViewById(R.id.desc);
			TextView amountTx = (TextView)view.findViewById(R.id.amount);
			
			
			descTx.setText("投标扣款总额：");
			amountTx.setText(Utils.amountConversionFormat(flowSummary.deduct)+"元");
			
			mSummaryLayout.addView(view);
		}
		
		if(flowSummary.withdraw!=0){
			LayoutInflater mInflater = LayoutInflater.from(this);
			View view = mInflater.inflate(R.layout.view_summary_item, null);
			TextView descTx = (TextView)view.findViewById(R.id.desc);
			TextView amountTx = (TextView)view.findViewById(R.id.amount);
			
			descTx.setText("提现总额：");
			amountTx.setText(Utils.amountConversionFormat(flowSummary.withdraw)+"元");
			
			mSummaryLayout.addView(view);
		}
		
		if(flowSummary.poundage!=0){
			LayoutInflater mInflater = LayoutInflater.from(this);
			View view = mInflater.inflate(R.layout.view_summary_item, null);
			TextView descTx = (TextView)view.findViewById(R.id.desc);
			TextView amountTx = (TextView)view.findViewById(R.id.amount);
			
			descTx.setText("提现手续费总额：");
			amountTx.setText(Utils.amountConversionFormat(flowSummary.poundage)+"元");
			
			mSummaryLayout.addView(view);
		}
		
		if(flowSummary.management!=0){
			LayoutInflater mInflater = LayoutInflater.from(this);
			View view = mInflater.inflate(R.layout.view_summary_item, null);
			TextView descTx = (TextView)view.findViewById(R.id.desc);
			TextView amountTx = (TextView)view.findViewById(R.id.amount);
			
			descTx.setText("提现管理费总额：");
			amountTx.setText(Utils.amountConversionFormat(flowSummary.management)+"元");
			
			mSummaryLayout.addView(view);
		}
		
		if(flowSummary.refundticket!=0){
			LayoutInflater mInflater = LayoutInflater.from(this);
			View view = mInflater.inflate(R.layout.view_summary_item, null);
			TextView descTx = (TextView)view.findViewById(R.id.desc);
			TextView amountTx = (TextView)view.findViewById(R.id.amount);
			
			descTx.setText("提现失败（退票）总额：");
			amountTx.setText(Utils.amountConversionFormat(flowSummary.refundticket)+"元");
			
			mSummaryLayout.addView(view);
		}
		
		mSummaryTime.setText(Utils.amountConversionFormat(flowSummary.back)+"元");
	}
}
